﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using System.Net;


namespace Future.Settings
{
    public class SystemConfig
    {
        private bool _AllowRunMultiInstance = false;
        private bool _DoubleClickOnEditMode = false;
        private string _SkinName = "Blue";
        private IPAddress _ServerIP = null;
        private int _ServerHttpPort = 0;
        private int _ServerTcpPort = 0;
        private int _ChannelType = 0; // 0 = Tcp ,1 = Http
        private int _PingMS = 30 * 1000;

        private static SystemConfig _CurrentConfig;

        public static SystemConfig CurrentConfig
        {
            get { return SystemConfig._CurrentConfig; }
            set { SystemConfig._CurrentConfig = value; }
        }

        static SystemConfig()
        {
            ReadSettings();
        }

        SystemConfig()
        {
            _ServerIP = IPAddress.Parse(Future.Management.Environment.GetLocalIPAddress());
        }

        /// <summary>
        /// 将对象写入XML文件
        /// </summary>
        /// <param name="config"></param>
        public static void WriteSettings(SystemConfig config)
        {
            XmlSerializer xs = new XmlSerializer(typeof(SystemConfig));
            Stream stream = new FileStream(AllPath.S_Path_SystemConfig, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xs.Serialize(stream, config);
            stream.Close();

        }

        /// <summary>
        /// 读取配置文件 
        /// </summary>
        public static void ReadSettings()
        {
            string path = AllPath.S_Path_SystemConfig;
            if (!File.Exists(path))
            {
                SystemConfig.CurrentConfig = new SystemConfig();
                WriteSettings(SystemConfig.CurrentConfig);
            }
            else
            {
                XmlSerializer xs = new XmlSerializer(typeof(SystemConfig));
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                CurrentConfig = (SystemConfig)xs.Deserialize(stream);
                stream.Close();
            }
        }

        /// <summary>
        /// 初始化系统配置
        /// </summary>
        public static void RestoreDefault()
        {
            SystemConfig.CurrentConfig = new SystemConfig();
            WriteSettings(SystemConfig.CurrentConfig);
        }

        /// <summary>
        ///运行单个实例 
        /// </summary>
        public bool AllowRunMultiInstance
        {
            get { return _AllowRunMultiInstance; }
            set { _AllowRunMultiInstance = value; }
        }

        /// <summary>
        /// 双击表格进入编辑模式
        /// </summary>
        public bool DoubleClickOnEditMode
        {
            get { return _DoubleClickOnEditMode; }
            set { _DoubleClickOnEditMode = value; }
        }

        /// <summary>
        /// 皮肤名称
        /// </summary>
        public string SkinName
        {
            get { return _SkinName; }
            set { _SkinName = value; }
        }

        public string ServerIP
        {
            get { return _ServerIP.ToString(); }
            set { _ServerIP = IPAddress.Parse(value); }
        }

        public int ServerHttpPort
        {
            get { return _ServerHttpPort; }
            set { _ServerHttpPort = value; }
        }

        public int ServerTcpPort
        {
            get { return _ServerTcpPort; }
            set { _ServerTcpPort = value; }
        }

        public int ChannelType
        {
            get { return _ChannelType; }
            set { _ChannelType = value; }
        }

        public int PingMS
        {
            get { return _PingMS; }
            set { _PingMS = value; }
        }
    }
}
